﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.CMS263.PO;
using Com.CMS263.BLL;
using Com.CMS263.Util;

namespace Com.CMS263.Web.manage
{
    public partial class emailset : System.Web.UI.Page
    {
        public string mail_server = "";
        public string mail_from = "";
        public string mail_frompass = "";
       
        EmailSetBLL bll = new EmailSetBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }
        public void BindData()
        {
            EmailSet entity = new EmailSet();
            try
            {
                entity = bll.GetSet()[0];
                TxtSmtp.Text = entity.Server;
                txtFrom.Text = entity.MailUser;
                txtFromPass.Text = entity.MailPass;
                hhid.Value = entity.Id.ToString();
            }
            catch (Exception)
            {
                TxtSmtp.Text = mail_server;
                txtFrom.Text = mail_from;
                txtFromPass.Text = mail_frompass;
            }
        }

        public string msg = "";
        protected void Button1_Click(object sender, EventArgs e)
        {
            string server = TxtSmtp.Text;
            string fromuser = txtFrom.Text;
            string userpass = txtFromPass.Text;
            if (string.IsNullOrEmpty(server))
            {
                StringUtil.Alert("发送服务器(SMTP)不能为空", "-1");
            }
            if (string.IsNullOrEmpty(fromuser))
            {
                StringUtil.Alert("发送邮箱", "-1");
            }
            if (!StringUtil.isEmail(fromuser))
            {
                StringUtil.Alert("发送邮箱格式错误", "-1");
            }
            if (string.IsNullOrEmpty(userpass))
            {
                StringUtil.Alert("发送邮箱密码", "-1");
            }
            int id = Convert.ToInt32(hhid.Value);
            if (id == 0)
            {
                EmailSet es = new EmailSet();
                es.MailPass = userpass;
                es.MailUser = fromuser;
                es.Server = server;
                bll.entity = es;
                if (bll.Add())
                {
                    StringUtil.Alert("修改成功", "-1");
                }
            }
            else
            {
                EmailSet es = new EmailSet();
                es.Id = id;
                es.MailPass = userpass;
                es.MailUser = fromuser;
                es.Server = server;
                bll.entity = es;
                if (bll.UpDate())
                {
                    StringUtil.Alert("修改成功", "-1");
                }
            }
        }
    }
}
